package com.hy.main.demo.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Author yang DongYong
 * @Date 2022/5/11 - 05 - 11 - 2:55
 * @Description: com.hy.main.demo.rabbitmq
 * @version: 1.0
 */
public class ReceiveLogsTopic01 {
    public static final String EXCHANGE_NAME = "topic_logs";
    public static final String QUEUE_NAME = "C1";

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitMqUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        // 绑定交换机
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"*.orange.*");
        System.out.println("等待接收消息.....");
        // 消息成功回调
        DeliverCallback deliverCallback = (tag, message) ->
                System.out.println("routingKey为:"+ message.getEnvelope().getRoutingKey()
                        +"，接收到的消息为:"+new String(message.getBody()));
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> System.out.println("取消消息"));
    }
}



 class ReceiveLogsTopic02 {
    public static final String EXCHANGE_NAME = "topic_logs";
    public static final String QUEUE_NAME = "C2";

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitMqUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        // 绑定交换机
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"lazy.#");
        System.out.println("等待接收消息.....");
        // 消息成功回调
        DeliverCallback deliverCallback = (tag, message) ->
                System.out.println("routingKey为:"+ message.getEnvelope().getRoutingKey()
                        +"，接收到的消息为:"+new String(message.getBody()));
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> System.out.println("取消消息"));
    }
}
